﻿using System;
using System.Linq;
using Loki.Game;
using Loki.Game.GameData;
using Loki.Game.Objects;
using Loki.Utilities;

namespace FlaskHelper
{
    class Helpers
    {
        public static Player Me { get { return LokiPoe.ObjectManager.Me; } }
        public static readonly WaitTimer FlaskCd = new WaitTimer(TimeSpan.FromSeconds(0.7));

        public static float RessourceToCheck { get { return FlaskHelperSettings.Instance.UseEnergyShieldThresholds ? Me.EnergyShieldPercent : Me.HealthPercent; }}

        public static void CheckFlaskUsage(Flasks.FlaskTypes flaskType, float flaskThreshold)
        {
            switch ((int)flaskType)
            {
                case 0: // Life
                    if (ShouldUseLifeFlask(0, flaskThreshold))
                        UseFlask(0);
                    break;
                case 1: // Mana
                    if (ShouldUseManaFlask(1, flaskThreshold))
                        UseFlask(1);
                    break;
                case 2: // Granite
                    if (ShouldUseGraniteFlask(2, flaskThreshold))
                        UseFlask(2);
                    break;
                case 3: // Jade
                    if (ShouldUseJadeFlask(3, flaskThreshold))
                        UseFlask(3);
                    break;
                case 4: // Ruby
                    if (ShouldUseRubyFlask(4, flaskThreshold))
                        UseFlask(4);
                    break;
                case 5: // Diamond
                    // TODO Add Diamond Flask Support
                    break;
                case 6: // Bleeding
                    if (ShouldUseBleedingFlask(6))
                        UseFlask(6);
                    break;
                case 7: // Shock
                    if (ShouldUseShockFlask(7))
                        UseFlask(7);
                    break;
                case 8: // Burning
                    if (ShouldUseBurningFlask(8))
                        UseFlask(8);    
                    break;
                case 9: // Frozen
                    if (ShouldUseFrozenFlask(9))
                        UseFlask(9);
                    break;
                case 10: // Divination
                    if (ShouldUseDivinationFlask(10))
                        UseFlask(10);
                    break;
            }
        }

        public static void UseFlask(int flaskId)
        {
            switch (flaskId)
            {
                case 0: // Life
                    Flasks.LifeFlasks.First().Use();
                    break;
                case 1: // Mana
                    Flasks.ManaFlasks.First().Use();
                    break;
                case 2: // Granite
                    Flasks.GraniteFlasks.First().Use();
                    break;
                case 3: // Jade
                    Flasks.JadeFlasks.First().Use();
                    break;
                case 4: // Ruby
                    Flasks.RubyFlasks.First().Use();
                    break;
                case 5: // Diamond
                    // TODO Add Diamond Flask Support
                    break;
                case 6: // Bleeding
                    Flasks.BleedingFlasks.First().Use();
                    break;
                case 7: // Shock
                    Flasks.ShockFlasks.First().Use();
                    break;
                case 8: // Burning
                    Flasks.BurningFlasks.First().Use();
                    break;
                case 9: // Frozen
                    Flasks.FrozenFlasks.First().Use();
                    break;
                case 10: // Divination
                    Flasks.DivinationDistillate.First().Use();
                    break;
            }

            FlaskCd.Reset();
        }

        #region Checks

        public static bool ShouldUseLifeFlask(int flaskId, float flaskThreshold)
        {
            return
                FlaskCd.IsFinished &&
                Me.HealthPercent < flaskThreshold &&
                Flasks.LifeFlasks.Count() != 0 &&
                !Me.HasAura(Flasks.FlaskAuras[flaskId]);
        }

        public static bool ShouldUseManaFlask(int flaskId, float flaskThreshold)
        {
            return
                FlaskCd.IsFinished &&
                Me.ManaPercent < flaskThreshold &&
                Flasks.ManaFlasks.Count() != 0 &&
                !Me.HasAura(Flasks.FlaskAuras[flaskId]);
        }

        public static bool ShouldUseGraniteFlask(int flaskId, float flaskThreshold)
        {
            return
                FlaskCd.IsFinished &&
                RessourceToCheck < flaskThreshold &&
                Flasks.GraniteFlasks.Count() != 0 &&
                !Me.HasAura(Flasks.FlaskAuras[flaskId]);
        }
        public static bool ShouldUseJadeFlask(int flaskId, float flaskThreshold)
        {
            return
                FlaskCd.IsFinished &&
                RessourceToCheck < flaskThreshold &&
                Flasks.JadeFlasks.Count() != 0 &&
                !Me.HasAura(Flasks.FlaskAuras[flaskId]);
        }
        public static bool ShouldUseRubyFlask(int flaskId, float flaskThreshold)
        {
            return
                FlaskCd.IsFinished &&
                RessourceToCheck < flaskThreshold &&
                Flasks.RubyFlasks.Count() != 0 &&
                !Me.HasAura(Flasks.FlaskAuras[flaskId]);
        }
        public bool ShouldUseDiamondFlask(int flaskId, float flaskThreshold)
        {
            return
                FlaskCd.IsFinished &&
                Flasks.DiamondFlasks.Count() != 0 /*&&
                !Me.HasAura(Flasks.FlaskAuras[flaskId])*/;
        }
        public static bool ShouldUseBleedingFlask(int flaskId)
        {
            return
                FlaskCd.IsFinished &&
                Flasks.BleedingFlasks.Count() != 0 &&
                Me.HasAura(Flasks.FlaskAuras[flaskId]);
        }
        public static bool ShouldUseShockFlask(int flaskId)
        {
            return
                FlaskCd.IsFinished &&
                Flasks.ShockFlasks.Count() != 0 &&
                Me.HasAura(Flasks.FlaskAuras[flaskId]);
        }
        public static bool ShouldUseBurningFlask(int flaskId)
        {
            return
                FlaskCd.IsFinished &&
                Flasks.BurningFlasks.Count() != 0 &&
                Me.HasAura(Flasks.FlaskAuras[flaskId]);
        }
        public static bool ShouldUseFrozenFlask(int flaskId)
        {
            return
                FlaskCd.IsFinished &&
                Flasks.FrozenFlasks.Count() != 0 &&
                Me.HasAura(Flasks.FlaskAuras[flaskId]);
        }

        public static bool ShouldUseDivinationFlask(int flaskId)
        {
            var bt = Target.BestTarget;

            return
                FlaskCd.IsFinished &&
                Flasks.DivinationDistillate.Count() != 0 &&
                !Me.HasAura(Flasks.FlaskAuras[flaskId]) &&
                bt != null &&
                bt.Rarity == Rarity.Unique &&
                bt.HealthPercent > 8;
        }

        #endregion

    }
}
